﻿//using Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Logistics.bear;
//using Abp.Application.Services.Dto;

using AutoMapper;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Application.Dtos;


namespace Logistics.Storage
{
    public class WarehousingAppService : ApplicationService, IWarehousingAppService
    {
        private readonly IRepository<Warehousing, int> _WarehousingRepository;
        private readonly IRepository<MaterialType, int> _materialTypes;
        private readonly LogisticsDbContext _logisticsDbContext;

        public WarehousingAppService(IRepository<Warehousing, int> WarehousingRepository, IRepository<MaterialType, int> materialTypes, LogisticsDbContext logisticsDbContext)
        {
            _logisticsDbContext = logisticsDbContext;
            _WarehousingRepository = WarehousingRepository;
            _materialTypes = materialTypes;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Procurement"></param>
        /// <returns></returns>
        public async Task<WarehousingDto> CreateAsync(WarehousingDto Procurement)
        {
            var warehousing = new Warehousing
            {
                Name = Procurement.Name,
                Num = Procurement.Num,
                Material = Procurement.Material,
                Origin = Procurement.Origin,
                PaymentMethod = Procurement.PaymentMethod,
                Price = Procurement.Price,
                ProcurementTypeId = Procurement.ProcurementTypeId,
                Registrant = Procurement.Registrant,
                Remarks = Procurement.Remarks,
                Specifications = Procurement.Specifications,
                CreationTime = DateTime.Now
            };

            await _WarehousingRepository.InsertAsync(warehousing);
            return ObjectMapper.Map<Warehousing, WarehousingDto>(warehousing);
        }

        [RemoteService(false)]
        public async Task DeleteAsync(int id)
        {
            await _WarehousingRepository.DeleteAsync(id);
        }

        public async Task<WarehousingDto> GetAsync(int id)
        {
            var warehousing = await _WarehousingRepository.GetAsync(id);
            return ObjectMapper.Map<Warehousing, WarehousingDto>(warehousing);
        }

        public async Task<PagedResultDto<WarehousingDto>> GetListAsync(GetWarehousingListDto dto)
        {
            //var all = _ProcurementRepository.ToList();
            var all = _logisticsDbContext.Warehousing.ToList();
            var type = _logisticsDbContext.MaterialType.ToList();
            var procType = from a in all
                           join c in type on a.ProcurementTypeId equals c.Id
                           select new Warehousing
                           {
                               Id=a.Id,
                               Name = a.Name,
                               Num = a.Num,
                               TypeName=c.Name,
                               Material=a.Material,
                               Origin=a.Origin,
                               Price=a.Price,
                               PaymentMethod=a.PaymentMethod,
                               Registrant=a.Registrant,
                               Remarks=a.Remarks,
                               ProcurementTypeId=a.ProcurementTypeId,
                               Specifications=a.Specifications,
                               CreationTime=a.CreationTime,
                           };
            int totoCount = procType.Count();
            var data = new PagedResultDto<WarehousingDto>();
            data.TotalCount = totoCount;
            procType = procType.Skip((dto.page - 1) * dto.limit).Take(dto.limit).ToList();
            data.Items = ObjectMapper.Map<List<Warehousing>, List<WarehousingDto>>(procType.ToList());
            return data;
        }

        public async Task<List<GetMaterialTypeDto>> GetMaterialAsync()
        {
            var list = _materialTypes.ToList();
            return ObjectMapper.Map<List<MaterialType>, List<GetMaterialTypeDto>>(list);
        }

        public int UpdateAsync(WarehousingDto Procurement)
        {
            var upwarehousing = new Warehousing
            {
                Name= Procurement.Name,
                Num= Procurement.Num,
                TypeName=Procurement.TypeName,
                Material=Procurement.Material,
                Origin  =Procurement.Origin,
                PaymentMethod=Procurement.PaymentMethod,
                Price=Procurement.Price,
                ProcurementTypeId=Procurement.ProcurementTypeId,
                Registrant=Procurement.Registrant,
                Remarks=Procurement.Remarks,
                Specifications=Procurement.Specifications,
                CreationTime=DateTime.Now,
                Id=Procurement.Id,
            };
            _logisticsDbContext.Entry(upwarehousing).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            var i = _logisticsDbContext.SaveChanges();
            return i;
        }
    }
}
